package com.xiangyang.pos.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.xiangyang.pos.R;

/**
 * Created by Administrator on 2017/8/17 0017.
 *
 * 扫码
 *
 * @author YeDalin
 *
 */

public class ScanActivity extends Activity {

    private TextView tvGoBack;
    private TextView tvFinish;
    private DecoratedBarcodeView dbvScan;
    private CaptureManager capture;
    private TextView text;//暂时没用

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        initView();

        capture=new CaptureManager(this,dbvScan);
        capture.initializeFromIntent(getIntent(),savedInstanceState);
        capture.decode();


        setMyClick();
    }

    private void initView(){
        tvGoBack=findViewById(R.id.tv_go_back);
        tvFinish=findViewById(R.id.tv_finish);
        dbvScan=findViewById(R.id.dbv_scan);
        text=findViewById(R.id.text);
    }

    private void setMyClick(){
        tvGoBack.setOnClickListener(listener);
        tvFinish.setOnClickListener(listener);
    }


    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.tv_go_back:
                    finish();
                    break;
                case R.id.tv_finish:
                    finish();
                    break;
            }
        }
    };




    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return dbvScan.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

}
